<script lang="ts" setup>
import { User } from '@element-plus/icons-vue';
import type { FormInstance, FormRules } from 'element-plus'
import { reactive, onMounted, ref } from 'vue'
import { useUserStore } from '@/stores';

import { userUpdateService } from '@/api/user';

const userStore = useUserStore()
const user = ref()
onMounted(() => {
    user.value = userStore.user
    ruleForm.username = user.value.username;
    ['name', 'email', 'gender', 'age'].forEach(key => {
        ruleForm[key] = user.value[key];
    }
    )
})
const ruleForm: { [key: string]: any } = reactive({
    username: '',
    name: '',
    email: '',
    gender: '0',
    age: 0,
});


const ruleRef = ref<FormInstance>()
const rules = reactive<FormRules<typeof ruleForm>>({
    email: [{ type: 'email', trigger: 'blur' }],

})

const updateInfo = async (form: FormInstance | undefined) => {
    if (!form) return
    await form.validate(async (valid) => {
        if (valid) {
            const { data } = await userUpdateService(ruleForm)
            ElMessage.success(data.message)
            userStore.setUser(ruleForm)
        } else {
            ElMessage.warning('请检查输入')
        }
    })
}
</script>
<template>
    <el-card style="max-width: 480px">
        <template #header>
            <div class="card-header">
                <el-icon>
                    <User />
                </el-icon>
                <span>基本资料</span>
            </div>
        </template>
        <el-form ref="ruleRef" :rules="rules" :model="ruleForm" label-width="auto">
            <el-form-item prop="username">
                <template #label><el-text>Username: </el-text></template>
                <el-input v-model="ruleForm.username" disabled />
            </el-form-item>
            <el-form-item prop="name">
                <template #label><el-text type="primary">姓名：</el-text></template>
                <el-input v-model="ruleForm.name" />
            </el-form-item>
            <el-form-item prop="email">
                <template #label><el-text type="primary">邮箱：</el-text></template>
                <el-input v-model="ruleForm.email" />
            </el-form-item>
            <el-form-item prop="gender">
                <template #label><el-text type="primary">性别：</el-text></template>
                <el-radio-group v-model="ruleForm.gender">
                    <el-radio value="0" border>保密</el-radio>
                    <el-radio value="1" border>男</el-radio>
                    <el-radio value="2" border>女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item prop="age">
                <template #label><el-text type="primary">年龄：</el-text></template>
                <el-input-number v-model="ruleForm.age" :min="9" :max="100" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button type="success" @click="updateInfo(ruleRef)">更新信息</el-button>
        </template>
    </el-card>
</template>
<style lang="scss" scoped>
.el-card {
    min-width: 60%;
    margin-left: auto;
    margin-right: auto;

    .card-header {
        font-size: 20px;

        .el-icon {
            margin-right: 15px;
            font-size: 24px
        }
    }

    .el-form {
        .el-form-item {
            font-size: 16px;
        }
    }

    .el-button {
        height: 40px;
        width: 150px;
        font-size: 16px;
    }
}
</style>